/**
 * Created by hcadmin on 16/4/18.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function ($, IO, upui, up, jui, bootbox, definebt) {
    $(function () {

        //动态新增栏目
        $(document).on('click','.addone',function(){
           //alert($(this).css('color'));
            var count= $(".rows .row").length+1;
            var rowscount=$("tbody tr").length;
            console.log('count',count);
            console.log('rowscount',rowscount);

            if(count+rowscount>=9){
                bootbox.alert('最多只可添加8个栏目');
                return;
            }

            $(this).parent().parent().parent().append(
                '<div class="row" style="padding-top: 10px;">'+
                '<div class="col-md-2 col-md-offset-1" style="text-align: right;line-height: 26px;">'+
                '<span>栏目'+count+'</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<input style="width: 100%;" type="text" />'+
                '</div>'+
                '<div class="col-md-3" style="line-height: 26px;">'+
                '<a class="addone" href="#" style="color: #6ab7ad;">添加</a>&nbsp;&nbsp;'+
                    '<a class="removeone" href="#" style="color: red;">删除</a>'+
                '</div>'+
                '</div>'
            );
        });

        //动态删除栏目
        $(document).on('click','.removeone',function(){
            $(this).parent().parent().remove();
        });

        //增加栏目,调api
        $("#add_column").click(function(){
            var rowscount=$("tbody tr").length;

            //alert(rowscount)
            if(rowscount>=8){
                bootbox.alert('栏目已满8条,不能再添加');
                return;
            }
            var catename='';
            var isPass=true;
            //拿到所有input里面的值
            $(".rows .row input[type=text]").each(function(){

                if($(this).val().length<=0){
                    //bootbox.alert('请输入完整的栏目信息');
                    isPass=false;
                }
                if(isPass){
                    //拼接成字符串
                    catename+=$(this).val()+';';
                }
            });
            if(catename.length>0){
                catename=catename.substring(0,catename.length-1);
            }

            IO.post('/api/artcate/add',{catenames:catename},function(d){
                if(d.code==0){
                    bootbox.alert('添加成功');
                    window.location.href='/96053/column';
                }

            })

        });

        //变为可编辑
        $(document).on('click','.edit_td',function(){
            $(this).parents('tr').find('input').removeAttr('readonly');
            $(this).parents('tr').find('input').focus();
        });

        //失去焦点时触发,修改
        $("tr input").blur(function(){
            var obj=$(this).parents('tr').find('input');
            var catename=$(this).parents('tr').find('input').val();
            var id=$(this).parents('tr').find('span').attr('act_id');

            var parmas={};
            parmas.catename=catename;
            parmas.id=id;

            IO.post('/api/artcate/edit',parmas,function(d){
                if(d.code==0){
                    window.location.href='/96053/column';

                }
            })

        });


        //删除栏目
        $(document).on("click",".delete_td",function(){
            var act_id=$(this).attr("act_id");
            var data=eval("(" + localStorage.getItem('data') + ")");
            var admin_id=data.data.admin_id;

            if(act_id){
                bootbox.confirm("确定要删除吗？",function(result){
                    if(result) {
                        IO.post("/api/artcate/delete",{id:act_id,admin_id:admin_id},function(d){
                            //console.log(d);
                            if(d.code==0){
                                bootbox.alert('删除成功');
                                window.location.href='/96053/column';
                            }
                        });
                    }
                });
            }
        });
    });

});

